<template>
	<view>
		<view class="fixed-top bg-white pl-3 pr-3">
			<!-- <u-navbar :is-back="true" :is-fixed="true" title="积分任务"></u-navbar> -->
			<view class="dfex py-3">
				<view class="font-16 font-weight-bold">积分任务</view>
				<view class="font-14 font-weight-bold-500">我的积分：<text
						class="font-16 font-weight-bold- text-FFC251 font-weight-bold">{{notices}}</text></view>
			</view>
		</view>
		<view class="pl-2 pr-2" style="margin-top: 40px;">

			<scroll-view ref="scrollView" scroll-y scroll-with-animation style="height: calc(100vh - 0px);">
				<view v-for="(item, index) in brandPointsList" :key="index" class="show-sl mt-2 dfex">
					<view class="dfex py-5 pl-2 pr-2">
						<view class="iconfont icon-faqi text-FFC251 font-48 "></view>
						<view class="ml-2 lineHight-2">
							<view v-if="item.type === '1'" class="text-101010 font-6">完善企业主体基本信息</view>
							<view v-if="item.type === '2'" class="text-101010 font-16">完善基地信息</view>
							<view v-if="item.type === '3'" class="text-101010 font-16">发布1条产品信息</view>
							<view v-if="item.type === '4'" class="text-101010 font-16">填写个人标签</view>
							<view v-if="item.type === '5'" class="text-101010 font-16">使用上下游商城1次1分（每月不超5分）</view>
							<view v-if="item.type === '6'" class="text-101010 font-16">发布上市简报信息1次5分</view>
							<view v-if="item.type === '7'" class="text-101010 font-16">发布上市预报信息1次10分</view>
							<view v-if="item.type === '8'" class="text-101010 font-16">每天登录1次1分（每月不超5分）</view>
							<view class="text-A0A0A0">奖励{{item.value}}积分</view>
						</view>
					</view>
					<view style="width: 80px; text-align: right;">
						<u-button v-if="item.type === '8'" shape="circle" size="mini"
							style="text-align: right; background-color: #78AB06; color: #fff;"
							@click="open(item.type, index)">每日签到</u-button>
						<u-button v-else shape="circle" size="mini"
							style="text-align: right; background-color: #78AB06; color: #fff;"
							@click="open(item.type, index)">去完成</u-button>
					</view>
				</view>
				<view v-if=" brandPointsList.length > 0" class="text-center mt-3 mb-3 pb-3">
					<view class="text-muted">没有更多数据了</view>
				</view>
				<view v-else-if=" brandPointsList.length === 0" class="text-center mt-3 mb-3 pb-3">
					<view class="text-muted">暂无数据</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import $H from '@/common/free-lib/request.js';
	export default {

		data() {
			return {
				notices: {},
				result: '',
				brandPointsList: [],
				clicked: true,
			}
		},
		onShow() {
			this.NewDataList();
			this.brandPoints();
			this.brandPointsList = [];
			this.notices = []
		},
		onLoad() {
			this.NewDataList();
			this.brandPoints();
			this.brandPointsList = [];
			this.notices = []
		},

		methods: {
			// 我的数据
			NewDataList() {
				$H.get('/basisInform/integral/agriculture/integralSignInTotal', {}, {}).then(res => {
					this.notices = res.result
				}).catch(error => {
					uni.showToast({
						title: "网络错误，请稍后再试",
						icon: 'none'
					});
				});
			},
			brandPoints() {
				$H.get('/basisInform/integral/agriculture/integralTaskList', {}, {}).then(res => {
					this.brandPointsList = res.result;
				}).catch(error => {
					console.log(err)
				});
			},
			open(type, index) {
				const typeToPath = {
					1: '/pages/tabbar/user/basic',
					2: '/pages/tabbar/user/base',
					3: '/pages/tabbar/user/productAddition',
					4: '/pages/tabbar/user/tag',
					5: '/pages/tabbar/market/index',
					6: '/pages/tabbar/index/ProductLaunch',
					7: '/pages/tabbar/index/prediction',
					8: ''
				};

				const path = typeToPath[type];
				this.NewDataList();
				this.brandPoints();
				this.brandPointsList = [];
				this.notices = []
				if (path) {
					if (type === '5') {
						uni.switchTab({
							url: '/pages/tabbar/market/index'
						})
					}
					uni.navigateTo({
						url: path,
						fail: (err) => {
							console.error('跳转失败：', err);
						}
					});
				} else {
					const lastClickTime = uni.getStorageSync('lastClickTime');
					const now = Date.now();
					// 计算距离上次点击的时间间隔（单位：毫秒）
					const interval = now - lastClickTime;
					// 一天的毫秒数
					const oneDay = 24 * 60 * 60 * 1000;

					if (interval >= oneDay || !lastClickTime) {
						// 如果距离上次点击已经超过一天，或者是第一次点击
						// 发送请求处理
						$H.get('/basisInform/integral/agriculture/dailySignIn', {}, {}).then(res => {
							if (res.code === 200) {
								uni.showToast({
									title: res.message,
									icon: 'none',
								});
								// 将对应的 item 的 clicked 属性设置为 true，使按钮变灰
								this.$set(this.brandPointsList[index], 'clicked', true);
								// 保存本次点击时间
								uni.setStorageSync('lastClickTime', now);
							} else {
								uni.showToast({
									title: res.message,
									icon: 'none',
								});
							}
						}).catch(error => {
							console.log(error);
						});
					} else {
						// 如果距离上次点击未超过一天，提示用户一天只能点击一次
						uni.showToast({
							title: '一天只能点击一次',
							icon: 'none',
						});
					}
				}
			}

		}
	}
</script>

<style>
</style>